﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HelperPrototype.OtherForms
{
    public partial class XemHangHoaForm : Form
    {
        private string _maHangHoa = "";

        public XemHangHoaForm(string maHangHoa)
        {
            InitializeComponent();
            _maHangHoa = maHangHoa;
        }

        private void XemHangHoa_Load(object sender, EventArgs e)
        {
            loadData();
        }

        private void loadData()
        {
            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
            _DB.HangHoa item = dc.HangHoas.Single(i => i.MaHangHoa == _maHangHoa);

            // Fill textboxs
            txtMaHangHoa.Text = item.MaHangHoa;
            txtName.Text = item.TenHangHoa;
            txtUnit.Text = item.DonVi;
            txtPurpose.Text = item.MucDich;
            txtDescription.Text = item.MoTa;
            if (item.NhomHangHoa != null)
                txtGroup.Text = item.NhomHangHoa.TenNhomHangHoa;
            txtMeasure.Text = item.DonVi;
            txtStatus.Text = item.TrangThai;
            txtNote.Text = item.GhiChu;

            // Fill grid
            dgvSupplier.Rows.Clear();
            int count = 1;

            var items = from i in item.HangHoa_NhaCungCaps
                        where i.Dat == true &&
                              i.PhieuDanhGiaNCC.deleted == false &&
                              i.PhieuDanhGiaNCC.DaDuyet == true
                        group i by i.NhaCungCap into g
                        let newest = g.Max(i => i.PhieuDanhGiaNCC.NgayTao)
                        select g.Where(i => i.PhieuDanhGiaNCC.NgayTao == newest).Single();
            foreach (var v in items)
            {
                _DB.HangHoa_NhaCungCap i = v;

                DataGridViewRow r = new DataGridViewRow();
                r.CreateCells(dgvSupplier);
                r.Cells[0].Value = count++;
                r.Cells[1].Value = i.NhaCungCap.TenNhaCungCap;
                r.Cells[2].Value = i.ChatLuong;
                r.Cells[3].Value = i.Gia;
                r.Cells[4].Value = i.ThoiGianGiaoHang;
                r.Cells[5].Value = i.HauMai;
                r.Cells[6].Value = i.HinhThucThanhToan;
                r.Cells[7].Value = i.GhiChu;

                dgvSupplier.Rows.Add(r);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
